﻿Ext.define('page.Role.Mod', {
    extend: 'Kwan.ex.ModuleInstance',
    metas: [
        { name: "NameCN", label: "角色名称", sort: 1, ex_list: { width: 150 }, ex_edit: { allowBlank: false, width: 250} },
        { name: "NameEN", label: "代码", sort: 5, ex_list: { width: 150 }, ex_edit: { allowBlank: false, width: 250} },
        { name: "Memo", label: "角色备注", sort: 5, ex_list: { width: 150 }, ex_edit: { allowBlank: false, width: 250} },
        { name: "Enable", label: "状态", sort: 5, ex_list: { width: 150, renderer: Kwan.colorEnable }, ex_edit: { xtype: 'SimpleCombo', ex_combo: { ds: Kwan.tipUser[0], def: 1 }, cfg: { allowBlank: false, labelWidth: 70, width: 250}} }
	    ],
    cfg: { start: 0, limit: 20, facade: 'facade.axd?s_action=Role', def: 'Role' },
    newData: [{ Cid: 0, Count: 1, NameCN: "", Price: 0}],
    roleID: '',
    moduleID: '',
    clickSelect: false,
    getGrid: function () {
        var me = this,
            sorters = [{ property: 'EntityID', direction: 'DESC'}];
        var grid = Ext.create('Kwan.ex.Grid', {
            id: 'gpRole',
            region: 'west',
            width: '100%',
            isSample: true,
            store: Kwan.createStore(this.metas, this.cfg, sorters),
            columns: Kwan.createColumn(this.metas),
            title: '角色列表',
            getQuery: function (clear) {
                var nameCN = Ext.getCmp('qNameCN').getValue();
                if (clear) {
                    nameCN = '';
                }
                if (nameCN.length > 0) {
                    this.store.proxy.extraParams = { query1: 'NameCN=' + nameCN };
                } else { this.store.proxy.extraParams = null; }
                this.store.load();
                this.filters.bindStore(this.store);
            },
            operBar: Ext.create('Kwan.ex.CrudBar', {
                pushItems: [
                    { scale: 'medium', height: 25, width: 100, emptyText: " 搜索房号..", xtype: 'textfield', id: 'qNameCN', name: 'qNameCN' },
                    { scale: 'medium', text: '搜索', icon: "/Admin/style/24Icons/jiancha.png", name: 'btnSearch',
                        handler: function () { Ext.getCmp('gpRole').getQuery(); }
                    }
                ]
            }),
            listeners: {
                itemclick: function () {
                    var selModel = grid.getSelectionModel();
                    var tree = Ext.getCmp('ModuleTree');
                    Ext.getCmp('gpRolePermit').disable();
                    if (Ext.isDefined(selModel.getSelection()[0])) {
                        tree.enable();
                        me.roleID = selModel.getSelection()[0].data.EntityID;
                        tree.store.proxy.url = 'facade.axd?s_action=ModuleTree&RoleID=' + me.roleID;
                        tree.store.load();
                    } else {
                        tree.disable();
                        Ext.getCmp('gpRolePermit').enable();
                    }
                }
            }
        });
        grid.operBar.addListener('operAdd', function handler() {
            me.newData[0].CreatedDate = new Date();
            var cfg = { operType: 'add', saveUrl: me.cfg.facade, loadUrl: null, objId: null },
                win = me.getWin(cfg);
            win.show();
        });
        grid.operBar.addListener('operEdit', function handler() {
            var id = GetGridSelectedRowData(grid).EntityID;
            var cfg = { operType: 'edit', saveUrl: me.cfg.facade, loadUrl: me.cfg.facade, objId: id };
            var win = me.getWin(cfg);
            win.show();
        });
        grid.operBar.addListener('operDel', function handler() {
            var selects = grid.getSelectionModel().getSelection(), ids = [];
            if (selects.length > 0) {
                Ext.each(selects, function (item) {
                    ids.push(item.data.EntityID);
                });
                var cfg = { url: me.cfg.facade, id: ids.join(","), store: grid.getStore() };
                DoDelete(cfg);
            } else {
                ShowInfoMsg('系统提示', '请至少选择一项！');
            }
        });
        grid.operBar.addListener('operRefresh', function handler() {
            grid.getStore().load();
        });
        grid.addListener('itemdblclick', function handler(grid, rec, item, index, e) {
            var cfg = { operType: 'edit', saveUrl: me.cfg.facade, loadUrl: me.cfg.facade, objId: rec.data.EntityID };
            var win = me.getWin(cfg);
            win.show();
        });
        return grid;
    },
    getWin: function (cfg) {
        var win = Ext.create('Kwan.ex.FormWin', {
            title: '角色管理',
            width: 300,
            height: 230,
            operType: cfg.operType, // 操作类型,
            fpID: this.cfg.def,
            formWinItems: Kwan.createFormItems(this.metas, this.cfg.def), // 表单包含的字段
            formColumns: 1, // 有几列
            //formWinFieldWidth: 800, // 字段的总宽度
            saveUrl: cfg.saveUrl, // 提交的url地址
            loadUrl: cfg.loadUrl, // 读取数据的Url地址
            fPanel: 'gpProduct', // 操作的父页面对象
            objId: cfg.objId, // 要修改的数据的id
            disableFields: [], // 编辑时要禁用的字段['Text', 'Code']
            newData: this.newData
        });
        return win;
    },
    getCenter: function () {
        var panel = Ext.create('Ext.Panel', {
            collapsible: true,
            hideCollapseTool: true,
            titleCollapse: true,
            split: true,
            region: 'center',
            border: false,
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            items: [this.getRoleTree(), this.gpPermit()]
        });
        return panel;
    },
    getRoleTree: function () {
        var me = this,
            nodeID = '';
        var tree = Ext.create('Kwan.ex.ClassTree', {
            id: 'ModuleTree',
            title: '系统菜单',
            width: '100%',
            flex: 1, disabled: true,
            ajaxUrl: 'facade.axd?s_action=ModuleTree',
            listeners: {
                itemclick: function (view, node, item, index) {
                    me.moduleID = node.internalId;
                    gpRolePermit = Ext.getCmp('gpRolePermit');
                    gpRolePermit.enable();
                    Ext.Ajax.request({
                        url: 'facade.axd', async: false,
                        params: { s_action: 'Permit', moduleID: node.internalId, RoleID: me.roleID },
                        success: function (response) {
                            me.clickSelect = false;
                            var data = eval('(' + response.responseText + ')'),
                                indexs = [],
                                selModule = gpRolePermit.getSelectionModel(),
                                selections = selModule.store.getRange();
                            selModule.deselectAll();
                            for (i = 0; i < selections.length; i++) {
                                if (data[i].Value === true) {
                                    indexs.push(selections[i]);
                                }
                            };
                            selModule.doMultiSelect(indexs);
                            me.clickSelect = true;
                        }
                    });
                },
                checkchange: function (node, checked) {
                    me.moduleID = node.internalId;
                    Ext.getCmp('gpRolePermit').enable();
                    Ext.Ajax.request({
                        url: 'facade.axd', async: false,
                        params: { s_action: 'CheckModule', moduleID: node.internalId, RoleID: me.roleID, check: checked }
                    });
                }
            }
        });
        tree.expandAll();
        return tree;
    },
    gpPermit: function () {
        var me = this,
            permitMetas = [{ name: "Text", label: "权限名称", sort: 1, ex_list: { width: 150, flex: 1}}],
            roleID = '52c81313a4b24750a23b566ce8553eba',
            permitCfg = { start: 0, limit: 20, facade: 'facade.axd?s_action=Permit&RoleID=' + roleID, def: 'Permit' },
            fields = ['Text', 'Value'],
            ds = Ext.create('Ext.data.Store', {
                autoLoad: true,
                fields: fields,
                proxy: { type: 'ajax', url: 'facade.axd?s_action=Permit&s_method=list&RoleID=' + roleID, reader: { type: 'json' }
                }
            });
        var grid = Ext.create('Kwan.ex.Grid', {
            id: 'gpRolePermit', flex: 1, width: '100%', isSample: true, store: ds, disabled: true, title: '权限列表',
            columns: [{ text: '权限名称', dataIndex: 'Text', flex: 1}],
            listeners: {
                selectionchange: function (selModule, selected) {
                    if (me.clickSelect) {
                        var indexs = ['0', '0', '0', '0', '0'];
                        for (i = 0; i < 5; i++) {
                            if (selected[i]) {
                                indexs[selected[i].index] = '1';
                            }
                        }
                        Ext.Ajax.request({
                            url: 'facade.axd', async: false,
                            params: { s_action: 'CheckPermit', moduleID: me.moduleID, RoleID: me.roleID, indexs: indexs.join("") }
                        });
                    }
                }
            }
        });
        return grid;
    },
    run: function () {
        var me = this;
        me.viewport.add([me.getGrid(), me.getCenter()]);
    }
});